﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using AutoPartsSearch.Domain.Abstract.Repositories;
using AutoPartsSearch.Domain.Abstract;
using AutoPartsSearch.Domain.Common;
using AutoPartsSearch.Domain.Entities;
using AutoPartsSearch.Domain.Entities.Enums;

namespace AutoPartsSearch.Domain.Concrete.Repositories
{
    public class DapperSearchQueryRepository : BaseRepository, ISearchQueryRepository
    {
        public DapperSearchQueryRepository(ILanguageGetter languageGetter) : base(languageGetter)
        {
        }

        public int Add(Entities.SearchQuery instance)
        {
            var id = -1;
            var param = new Dapper.DynamicParameters();
            param.Add("@Id", id, DbType.Int32, ParameterDirection.Output);
            param.Add("@PartName", instance.PartName, DbType.String);
            param.Add("@PartGroup", instance.PartGroup.Id, DbType.Int32);
            param.Add("@PartType", instance.PartType.Id, DbType.Int32);
            param.Add("@PartCatalogNumber", instance.PartCatalogNumber, DbType.String);
            param.Add("@PartState", instance.PartState.Id, DbType.Int32);
            param.Add("@PartDescription", instance.PartDescription, DbType.String);
            param.Add("@CarType", instance.CarType.Id, DbType.Int32);
            param.Add("@CarYear", instance.CarYear, DbType.Int16);
            param.Add("@CarBrand", instance.CarBrand.Id, DbType.Int32);
            param.Add("@CarModel", instance.CarModel, DbType.String);
            param.Add("@CarModification", instance.CarModification, DbType.String);
            param.Add("@CarVin", instance.CarVin, DbType.String);
            param.Add("@CarEngine", instance.CarEngine, DbType.String);

            param.Add("@CarCase", Converter.GetIdOrNull(instance.CarCase), DbType.Int32);
            param.Add("@CarTransmission", Converter.GetIdOrNull(instance.CarTransmission), DbType.Int32);
            param.Add("@CarDrive", Converter.GetIdOrNull(instance.CarDrive), DbType.Int32);
            param.Add("@CarFuelType", Converter.GetIdOrNull(instance.CarFuelType), DbType.Int32);

            param.Add("@City", instance.City.Id, DbType.Int32);
            param.Add("@Name", instance.Name, DbType.String);
            param.Add("@Phone", instance.Phone, DbType.String);
            param.Add("@Phone2", instance.Phone2, DbType.String);
            param.Add("@Email", instance.Email, DbType.String);
            param.Add("@Comment", instance.Comment, DbType.String);
            QueryExecutor.ExecuteProcedure("sp_AddSearchQuery", param);
            id = param.Get<int>("@Id");
            return id;
        }

        public Entities.SearchQuery GetById(int id)
        {
            var param = new Dapper.DynamicParameters();
            param.Add("@Id", id, DbType.Int32);
            param.Add("@Lang", this.CurrentLanguage, DbType.String);
            return QueryExecutor.QueryDynamic<SearchQuery>("sp_GetSearchQueryById", ModelReader, param).FirstOrDefault();
        }

        public IEnumerable<Entities.SearchQuery> Get(int page, int pageSize)
        {
            var param = new Dapper.DynamicParameters();
            param.Add("@Lang", this.CurrentLanguage, DbType.String);
            param.Add("@Page", page, DbType.Int32);
            param.Add("@PageSize", pageSize, DbType.Int32);
            return QueryExecutor.QueryDynamic<SearchQuery>("sp_GetSearchQueriesPaged", ModelReader, param);
        }

        public void Delete(int id)
        {
            var param = new Dapper.DynamicParameters();
            param.Add("@Id", id, DbType.Int32);
            param.Add("@Lang", this.CurrentLanguage, DbType.String);
            QueryExecutor.ExecuteProcedure("sp_DeleteSearchQuery", param);
        }

        public long GetTotalCount()
        {
            return QueryExecutor.QueryProcedure<int>("sp_GetSearchQueriesCount").First();
        }

        public void Update(Entities.SearchQuery instance)
        {
            throw new NotImplementedException();
        }


        public IEnumerable<SearchQuery> Get()
        {
            var param = new Dapper.DynamicParameters();
            param.Add("@Lang", this.CurrentLanguage, DbType.String);
            return QueryExecutor.QueryDynamic<SearchQuery>("sp_GetSearchQueries", ModelReader, param);
        }

        private void ModelReader(SearchQuery searchQuery, dynamic o)
        {
            searchQuery.Id = o.Id;
            searchQuery.PartName = o.PartName;
            searchQuery.PartGroup = new Dictionary(o.PartGroup);
            searchQuery.PartType = new Dictionary(o.PartType);
            searchQuery.PartCatalogNumber = o.PartCatalogNumber;
            searchQuery.PartState = new Dictionary(o.PartState);
            searchQuery.PartDescription = o.PartDescription;
            searchQuery.CarType = new Dictionary(o.CarType);
            searchQuery.CarYear = o.CarYear;
            searchQuery.CarBrand = new Dictionary(o.CarBrand);
            searchQuery.CarModel = o.CarModel;
            searchQuery.CarModification = o.CarModification;
            searchQuery.CarVin = o.CarVin;
            searchQuery.CarEngine = o.CarEngine;
            searchQuery.CarCase = new Dictionary(o.CarCase);
            searchQuery.CarTransmission = new Dictionary(o.CarTransmission);
            searchQuery.CarDrive = new Dictionary(o.CarDrive);
            searchQuery.CarFuelType = new Dictionary(o.CarFuelType);
            searchQuery.City = new Dictionary(o.City);
            searchQuery.Name = o.PartName;
            searchQuery.Phone = o.Phone;
            searchQuery.Phone2 = o.Phone2;
            searchQuery.Email = o.Email;
            searchQuery.Comment = o.Comment;
            searchQuery.Created = o.Created;
        }
    }
}
